<template>
	<view class="page">
		<!-- 页面头部内容开始 -->
		<vk-data-page-header
			:title="page.title"
			:show-back="page.showBack"
			@back="pageBack"
		></vk-data-page-header>
		<!-- 页面头部内容结束 -->
		<view class="page-body" v-loading="form1.props.loading" style="padding-bottom: 40px;">
			<!-- 页面主体内容开始 -->
			<vk-data-form
				ref="form1"
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				:loading.sync="form1.props.loading"
				:labelWidth="form1.props.labelWidth"
				:before-action="form1.props.beforeAction"
				:footer-show="false"
				:success-msg="form1.props.successMsg"
				@success="formSuccess"
			></vk-data-form>
			<!-- 页面主体内容结束 -->
		</view>
		<view class="page-footer">
			<!-- 页面底部内容开始 -->
			<el-button v-if="page.showBack" plain style="margin-right: 20px;width: 80px;" @click="pageBack" :loading="form1.props.loading">{{ page.cancelText }}</el-button>
			<el-button type="primary" style="width: 80px;" @click="submitForm" :loading="form1.props.loading">{{ page.submitText }}</el-button>
			<!-- 页面底部内容开始 -->
		</view>
		<view class="page-dialog">
			<!-- 页面弹窗内容开始 -->

			<!-- 页面弹窗内容开始 -->
		</view>
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例

	const shareRewardRules = [
		{ validator:function(rule, value, callback, obj){
			let data = that.form1.data;
			if((data.share_reward_1 + data.share_reward_2)>1){
				return callback(new Error('一级+二级返佣比例之和不能大于100%'));
			}
			callback();
		}, trigger: ["change","blur"] }
	];


	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				data:{

				},
				page: {
					title: "规则设置",
					submitText: "设置",
					cancelText: "返回",
					showBack: false,
					backPage:""
				},
				form1: {
					// 表单请求数据，此处可以设置默认值
					data: {
						mode:"none",
						apply_image:"",
						plan_exec_time:0,
						reward_lock_day:8,
						no_pay_allow_change_bind:0,
						examine_mode:0,
						is_help_superiors_sales:0,
						balance_recharge_reward_mode:0,
						balance_recharge_mode:0,
						share_reward_1:"",
						share_reward_2:"",
						examine_total_recharge_amount:"",
						examine_total_order_amount:"",
						examine_min_vip_lv:"",
						examine_goods_ids:[]
					},
					// 表单属性
					props: {
						// 表单请求地址
						action: "admin/business/sys.salesman.updateFxRule",
						// 表单字段显示规则
						columns: [
							{ key: "", title: "分销模式选择", type: "bar-title" },
							{ key: "mode", title: "模式", type: "radio",
								data: [
									{ value: "none", label: "关闭" },
									{ value: "fx2", label: "二级分销模式" },
								]
							},

							{ key: "", title: "二级分销默认返佣比例", type: "bar-title", showRule:"mode==fx2" },
							{ key: "share_reward_1", title: "一级比例", type: "percentage", width:200, showRule:"mode==fx2" },
							{ key: "share_reward_2", title: "二级比例", type: "percentage", width:200, showRule:"mode==fx2" },

							{ key: "", title: "审核设置", type: "bar-title" },
							{ key:"apply_intro", title:"申请介绍", type:"editor", width:"440px" },
							{ key: "examine_mode", title: "申请分销员是否需要审核", type: "radio",
								data: [
									{ value: 0, label: "无需审核" },
									{ value: 1, label: "人工审核" },
									{ value: 2, label: "自动审核" },
								]
							},
							{ key: "", title: "自动审核条件", type: "bar-title", showRule:"examine_mode==2" },
							{ key: "examine_total_recharge_amount", title: "累计充值余额", type: "money", width:160, rightText:"元", clearable:false, placeholder:"输入金额", showRule:"examine_mode==2" },
							{ key: "examine_total_order_amount", title: "累计订单金额", type: "money", width:160, rightText:"元", clearable:false, placeholder:"输入金额", showRule:"examine_mode==2" },
							{ key: "examine_min_vip_lv", title:"最低会员等级", placeholder:"会员等级", type:"select", width:140,
								showRule:"examine_mode==2",
								data:()=>{
									return this.vk.getVuex('$user.mchInfo.vip_level_rule') || [];
								},
								props:{ value:'lv', label:'name', children:'children' },
							},
							{
								key: "examine_goods_ids", title: "购买指定任意商品", type: "table-select", placeholder:"选择商品", showRule:"examine_mode==2", tips:"以上条件满足任意一项即可",
								action:"admin/business/sys.goods.getList",
								multiple: true,
								formData:{
									status:0
								},
								columns:[
									{ key:"sort", title:"排序", type:"number", width:80, defaultValue:'0', sortable:"custom" },
									{ key:"goods_thumb", title:"商品图", type:"avatar", imageWidth:40 },
									{ key:"name", title:"商品名称", type:"text", nameKey:true },
									{ key:"categoryList", title:"所属分类", type:"text", width:140, defaultValue:'暂无',
										formatter: (val, row, column, index) => {
											let str = "";
											val.map((item, index) => {
												if (index > 0) str += "、";
												str += item.name;
											});
											return str;
										}
									},
									{ key:"sku_list", title:"价格区间", type:"text", width:200, defaultValue:'暂无' ,
										formatter:(val, row, column, index) => {
											let a = [];
											val.map((item, index) => {
												a.push(Number(item.price))
											});
											let max = Math.max(...a);
											let min = Math.min(...a);
											if(max != min){
												// 如果最大值和最小值不相等
												return	vk.pubfn.priceFilter(min) +' - '+ vk.pubfn.priceFilter(max);
											}else{
												// 如果最大值和最小值相等
												return  vk.pubfn.priceFilter(min);
											}
										}
									},
									{ key: "stock", title: "商品库存", type: "number", width: 110, show:["row"]  },
									{ key:"view_count", title:"累计浏览量", type:"number", width:140, defaultValue:0, sortable:"custom" },
									{ key:"real_sell_count", title:"实际总销量", type:"number", width:140, defaultValue:0, sortable:"custom" },
									{ key:"_id", title:"商品ID", type:"text", idKey:true, show:["none"] }, // idKey:true 代表此字段为主键字段，若设置show:["none"],则可以在表格中隐藏该字段的显示
								],
								queryColumns:[
									{ key: "name", type: "text", title:"", placeholder:"请输入商品名称", width:180, mode:"%%"  },
									{
										key:"category_ids", title:"商品分类", type:"cascader", width:180, mode:"in",
										action:"admin/business/sys.goodsCategory.kh_getList",
										props:{
											list:"rows",
											value:"_id",
											label:"name",
											children:"children",
											checkStrictly: true,
											emitPath:false
										}
									}
								]
							},

							{ key: "", title: "订单结算设置", type: "bar-title" },
							{ key: "plan_exec_time", title: "订单延迟多少天结算", type: "number", labelWidth:220, width:160, rightText:"天", clearable:false, placeholder:"输入天数",
								tips: "默认0天，如果填0，则实时结算，如果填1，则当天晚上24点结算 如果填2，则次日晚上24点结算，以此类推。（延迟结算的作用是方便上级补单升级拿奖励）"
							},
							{ key: "reward_lock_day", title: "结算后，资金冻结多少天可提现", type: "number", labelWidth:220, width:160, rightText:"天", clearable:false, placeholder:"输入天数",
								tips: "默认8天，如果填0，则结算后马上可提现，如果填1，则结算后的当天晚上24点可提现 如果填2，则结算后的次日晚上24点结算，以此类推。（冻结的作用是用户可能会退款，导致奖励已被提现从而损失资金。）"
							},
							{ key: "", title: "提现设置", type: "bar-title" },
							{ key: "max_withdrawal_num", title: "每月可提现次数", type: "number", width:160, rightText:"次/月", clearable:false, placeholder:"输入次数", },
							{ key: "min_withdrawal_amount", title: "最少提现金额", type: "money", width:160, rightText:"元", clearable:false, placeholder:"输入金额", },
							{ key: "withdrawal_service_amount", title: "提现手续费", type: "percentage", width:160, clearable:false, placeholder:"请输入比例", tips:"默认1%，您可以自己设置向用户收取的比例，如果设置为0，则由您来承担手续费，建议设置1%-3%之间" },

							{ key: "", title: "抢客设置", type: "bar-title" },
							{ key: "no_pay_allow_change_bind", title: "未下单", type: "radio",
								tips:`禁止抢客：用户一旦被绑定，则不能再被其他分销员绑定。</br>用户未下单前允许抢客：在该用户被最近一个分销员绑定后的时间算起，该用户没有下单并付款，则可以被其他分销员绑定。`,
								data: [
									{ value: 0, label: "禁止抢客" },
									{ value: 1, label: "用户未下单前允许抢客" },
									{ value: 2, label: "始终允许抢客" },
								]
							},
							//{ key: "", title: "用户注册设置", type: "bar-title" },
							//{ key: "", title: "其他设置", type: "bar-title" },
							//{ key: "url_scheme_salesman_center", title: "业务中心urlScheme", type: "text" },

						],
						// 表单验证规则
						rules: {
							share_reward_1:shareRewardRules,
							share_reward_2:shareRewardRules
						},
						labelWidth: "180px",
						loading:false,
						successMsg:"设置成功",
						beforeAction:function(formData){
							// 可在此处修改 formData 后返回 formData，若在此处return false，则表单不触发提交请求。
							return {
								fx:formData
							};
						},
					}
				}
			}
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){

		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				that.$nextTick(function(){
					vk.pubfn.objectAssign(that.form1.data, vk.getVuex('$user.mchInfo.fx'));
					vk.callFunction({
						url: 'client/pub.getMchInfo',
						loading:{ that, name:"form1.props.loading"},
						data:{

						},
						success:function(data){
							vk.setVuex('$user.mchInfo',data.mchInfo);
							let fx = vk.pubfn.getData(data.mchInfo, "fx");
							if(fx){
								vk.pubfn.objectAssign(that.form1.data, fx);
							}
						}
					});
				});
			},
			// 页面跳转
			pageTo(path){
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(that.page.backPage) {
					vk.navigateTo(that.page.backPage);
				}
			},
			// 表单重置
			resetForm() {
				that.$refs.form1.resetForm();
			},
			// 表单提交
			submitForm(){
				that.$refs.form1.submitForm();
			},
			formSuccess(res){
				vk.setVuex("$user.mchInfo", res.data.mchInfo);
			}
		},
		// 监听器
		watch:{
			"form1.data.balance_transfer_rule": {
				handler: function(newVal=[], oldVal=[]) {
					if (newVal.indexOf("all") > -1 && oldVal.indexOf("all") == -1){
						that.form1.data.balance_transfer_rule = ["all","upper","lower","lower-lower","upper-upper","other"];
					} else if (newVal.indexOf("all") == -1 && oldVal.indexOf("all") > -1){
						that.form1.data.balance_transfer_rule = [];
					} else if (newVal.length == 5 && newVal.indexOf("all") == -1){
						that.form1.data.balance_transfer_rule = ["all","upper","lower","lower-lower","upper-upper","other"];
					} else if (newVal.length < 6 && newVal.indexOf("all") > -1){
						that.form1.data.balance_transfer_rule.splice(newVal.indexOf("all"),1);
					}
				},
			},
			"form1.data.balance_recharge_reward_mode": {
				handler: function(newVal, oldVal) {
					let that = this;
					if (newVal === 1) {
						// 使用时返
						that.form1.data.balance_recharge_mode = 0;
						that.form1.data.balance_recharge_mode = 0;
					} else {
						// 充值时返

					}
				},
			},

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
